<?php
cropimage(96, 96, 'digital_flag_3.jpg', 'out.jpg');
include("twitter_cred.php");

function cropimage($nw, $nh, $source, $dest) {

	/* Read the image, takes forever.- */
	$im = new Imagick($source);
	$size = getimagesize($source);
	$w = $size[0];
	$h = $size[1];

//get random location, rand returns integers, max set to actual image dimensions for good resolution.

	$x = rand(0,$w)/$w*($w-$nw);
	$y = rand(0,$h)/$h*($h-$nh);

$im->cropImage($nw, $nh, $x, $y);

$im->writeImage($dest);

/* Twitter part
from: http://apiwiki.twitter.com/REST+API+Documentation

account/update_profile_image
Updates the authenticating user's profile image.  Expects raw multipart data, not a URL to an image.
URL: http://twitter.com/account/update_profile_image.format
Formats: xml, json
Method(s): POST
Parameters:
    * image.  Required.  Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size.  Images with width larger than 500 pixels will be scaled down. 
*/

$curlbacktick = `curl -F 'image=@out.jpg' -H 'Expect:' -u $username:$password http://twitter.com/account/update_profile_image.xml`;
echo "<pre>$curlbacktick</pre>";
//added twitter_cred.php variables to back tick


// from http://morethanseven.net/2007/01/20/posting-to-twitter-using-php/
/*
// Set username and password
$username = '2pm';
$password = 'force5';
// The message you want to send
$message = 'testing. pay no attention';
// The twitter API address
//$url = 'http://twitter.com/statuses/update.xml';
$url = 'http://twitter.com/account/update_profile_image.xml';
// Alternative JSON version
// $url = 'http://twitter.com/statuses/update.json';
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
//curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
($curl_handle, CURLOPT_POSTFIELDS, "image=@$dest");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
// check for success or failure
if (empty($buffer)) {
    echo 'fail';
} else {
    echo 'success---';
}
*/

echo "Success motherfucker!\n";
}
?>
